package com.interview.javabase.algorithm;

/**
 * @author www.byteblogs.com
 * @date 2021-08-29
 * @since 2.0
 */
public class 右侧边界的二分查找 {
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,6,4,8,11,24,56,32};

        int i = right_bound(nums, 6);
        System.out.println(i);
    }

   public static int right_bound(int[] nums, int target) {
        if (nums.length == 0) return -1;
        int left = 0, right = nums.length;

        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                left = mid + 1; // 注意
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid;
            }
        }
        return left - 1; // 注意
    }
}
